function htmlFilter(str) {
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ /g, "&nbsp;").replace(/\"/g,
      "&quot;")
}
function isNumber(str) {
  return str != "" && !isNaN(str)
}
function isPositiveInteger(str) {
  return isNumber(str) && parseInt(str) > 0
}
function isNonnegativeInteger(str) {
  return isNumber(str) && parseInt(str) >= 0
}
function disableAllFormButtons(disabled) {
  for ( var i = 0, n = document.forms.length; i < n; i++) {
    var objForm = document.forms[i];
    for ( var j = 0, m = objForm.length; j < m; j++) {
      var objElement = objForm.elements[j];
      var strElementType = objElement.type.toLowerCase();
      if (strElementType == "submit" || strElementType == "button" || strElementType == "select-one") {
        objElement.disabled = disabled
      }
    }
  }
}
function checkAll(singleCheckboxName, allCheckboxObj) {
  var singleCheckboxes = document.getElementsByName(singleCheckboxName);
  for (i = 0; i < singleCheckboxes.length; i++) {
    singleCheckboxes[i].checked = allCheckboxObj.checked;
  }
}

function selectAllByButton(singleCheckboxName,flag) {
  var singleCheckboxes = document.getElementsByName(singleCheckboxName);
  for (i = 0; i < singleCheckboxes.length; i++) {
    singleCheckboxes[i].checked = flag;
  }
}


function checkOne(singleCheckboxObj, allCheckboxId) {
  var selectAll = document.getElementById(allCheckboxId);
  if (!singleCheckboxObj.disabled && !singleCheckboxObj.checked && selectAll.checked) {
    selectAll.checked = false;
    return
  }
  var selectOne = document.getElementsByName(singleCheckboxObj.name);
  var i = 0;
  var length = selectOne.length;
  for (; i < length; i++) {
    if (!selectOne[i].checked) {
      break;
    }
  }
  if (i >= length) {
    selectAll.checked = true;
  }
}
function view(url) {
  location.href = url;
}
function statWordCount(field,flag) {
  var stat = document.getElementById(field.name + "Stat");
  if (stat) {
    stat.innerHTML = flag?getlengthSize(field.value):getAbsoluteLength(field.value);
  }
}

function getlengthSize(str){
  var len = 0;
  for ( var i = 0; i < str.length; i++) {
    len++;
  }
  return len;
}
function getAbsoluteLength(str) {
  var len = 0;
  for ( var i = 0; i < str.length; i++) {
    str.charCodeAt(i) > 255 ? len += 2 : len++;
  }
  return len;
}
function setElementValue(elementId, value) {
  var e = document.getElementById(elementId);
  if (!e) {
    return
  }
  var type = e.type.toLowerCase();
  if (type == "select-one") {
    for ( var i = 0, n = e.length; i < n; i++) {
      if (e[i].value == value) {
        e[i].selected = true;
        break;
      }
    }
  } else if (type == "checkbox" || type == "radio") {
    if (e.value == value) {
      e.checked = true;
    }
  }
}
function highlight(e) {
  if (e != "inbox") {
    _updateElementStyle("inbox", false);
  }
  if (e != "sendbox") {
    _updateElementStyle("sendbox", false);
  }
  if (e != "draftbox") {
    _updateElementStyle("draftbox", false);
  }
  if (e != "trashbox") {
    _updateElementStyle("trashbox", false);
  }
  if (e != "educationMessageInbox") {
    _updateElementStyle("educationMessageInbox", false);
  }
  if (e != "pointMessageInbox") {
    _updateElementStyle("pointMessageInbox", false);
  }
  
  _updateElementStyle(e, true);
}
function _updateElementStyle(e, light) {
  var td = window.parent.document.getElementById(e + "_td");
  var a = window.parent.document.getElementById(e + "_a");
  if (td) {
    td.className = light ? "white" : "";
  }
  if (a) {
    a.className = light ? "white" : "";
  }
}
function openQuerySchoolId(schoolId, formName, unAutoSubmit, levels) {
  var url;
  var postfix = "showSchoolInfo.htm";
  var split = "/";
  var upTop = "..";
  var prefix = "";
  if (!levels) {
    levels = 1;
  }
  for ( var i = 0, j = levels; i < j; i++) {
    prefix = prefix + upTop + split;
  }
  url = prefix + postfix;
  var myArgs = window.showModalDialog(url, "", "dialogWidth:600px;dialogHeight:505px;dialogLeft:220px;dialogTop:205");
  if (myArgs != null) {
    document.getElementsByName(schoolId)[0].value = myArgs.toString();
    if (!unAutoSubmit) {
      if (document.getElementById(formName)) {
        document.getElementById(formName).submit();
      } else if (document.getElementsByName(formName)) {
        document.getElementsByName(formName)[0].submit();
      }
    }
  }
}
function onChangeSchoolId(schoolId) {
  var titleMain = document.getElementById("titleMain");
  var titleMainInnerHTML = titleMain.innerHTML;
  var value;
  if (titleMainInnerHTML.indexOf("?") == -1) {
    value = titleMainInnerHTML.replace(/\.[A-Za-z]{3}/g, ".htm?schoolId=" + schoolId);
  } else {
    value = titleMainInnerHTML.replace(/\?([A-Za-z]{8})\=(\d{6})/g, "?schoolId=" + schoolId);
  }
  document.getElementById("titleMain").innerHTML = value;
}
function setHome(a, url) {
  a.style.behavior = "url(#default#homepage)";
  a.setHomePage(url);
}
function addFavorite(title) {
  window.external.AddFavorite(window.document.location, title);
}
function addFavorite(url, title) {
  window.external.AddFavorite(url, title);
}
function startMarquee(lh, speed, delay, mrqId) {
  var t;
  var p = false;
  var o = document.getElementById(mrqId);
  o.innerHTML += o.innerHTML;
  o.onmouseover = function() {
    p = true;
  };
  o.onmouseout = function() {
    p = false;
  };
  o.scrollTop = 0;
  function start() {
    t = setInterval(scrolling, speed);
    if (!p) {
      o.scrollTop += 2;
    }
  }
  function scrolling() {
    if (o.scrollTop % lh != 0) {
      o.scrollTop += 2;
      if (o.scrollTop >= o.scrollHeight / 2) {
        o.scrollTop = 0;
      }
    } else {
      clearInterval(t);
      setTimeout(start, delay);
    }
  }
  setTimeout(start, delay);
}
function findPosX(obj) {
  var curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) {
    curleft += obj.x;
  }
  return curleft;
}
function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if (obj.y) {
    curtop += obj.y;
  }
  return curtop;
}
function fullEscape(text) {
  var encodedHtml = encodeURIComponent(text);
  return encodedHtml;
}
function statWordCountForSms(field, id, isSupport, length, isVisible) {
  var stat = document.getElementsByName(id + "Stat")[0];
  if(!stat){
    stat = document.getElementById(id + "Stat");
  }
  if (stat) {
    stat.innerHTML = getAbsoluteLengthForSms(field.value, id, isSupport, length, isVisible);
  }
}
function getAbsoluteLengthForSms(str, id, isSupport, length, isVisible) {
  var len = length;
  for ( var i = 0; i < str.length; i++) {
    len++;
  }
  
  var obj = document.getElementsByName(id)[0];
  if(!obj){
    obj = document.getElementById(id);
  }
  var warnObj = document.getElementsByName(id + "_warn")[0];
  if(!warnObj){
    warnObj = document.getElementById(id + "_warn");
  }
  if (len > 70 && isVisible) {
    if (isSupport) {
      warnObj.style.visibility = "visible";
      obj.style.border = "1px solid orange";
    } else {
      warnObj.style.display = "";
      obj.className = "blueTextareaForSms";
    }
  } else {
    if (isSupport) {
      warnObj.style.visibility = "hidden";
      obj.style.border = "1px solid gray";
    } else {
      warnObj.style.display = "none";
      // obj.className = "blueTextarea";
    }
  }
  return len;
}

function statWordCount1(field) {
  var stat = document.getElementById(field.name + "Stat");
  if (stat) {
    stat.innerHTML = getLength(field.value);
  }
}
function getLength(str) {
  var len = 0;
  for ( var i = 0; i < str.length; i++) {
    len++;
  }
  return len;
}
function insertAtCursor(a, b) {
  a.focus();
  if (document.selection) {
    var c = document.selection.createRange();
    c.text = b;
  } else if (a.setSelectionRange) {
    var d = a.selectionStart;
    var e = a.selectionEnd;
    var f = a.value.substring(0, d);
    var g = a.value.substring(e);
    a.value = f + b + g;
    a.setSelectionRange(e + b.length, e + b.length)
  } else {
    a.value += b
  }
}
function hideContextmenu() {
  window.event.returnValue = false;
}
function forbidContextmenu() {
  document.oncontextmenu = new Function("event.returnValue=false");
  document.onselectstart = new Function("event.returnValue=false");
}

function checkRec(filed,id){
  var stat = document.getElementById(id + "Stat");
  if(stat==null){
    return;
  }
  if(filed.value==null || filed.value.replace(new RegExp(" ","gm"),"")==""){
      stat.innerHTML="0"; 
  }
  
  var ary = filed.value.replace(new RegExp("，","gm"),","). split(",");  
  // 去重
  var json = {};  
  for (var i = 0; i < ary.length; i++) {
    var ary_i = ary[i].replace(new RegExp(" ","gm"),"");
  	  if(ary_i!=""){
    	json["index"+ary_i] = ary_i;  
  	  } 
  }
  
  var num=0;
  for (var key in json) {  
    num++;  
  }
  stat.innerHTML=num; 
}

// 判断日期格式是否正确(yyyy-MM-dd)(bim)
function isDate(date){ 
  var reg=/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/;
  if (!reg.test(date)){ 
    return false;
  } 
  return true;
}